# ...... ---------------------------------------------------------------
# VIDEOS ---------------------------------------------------------------

# Videos - basic ---------------------------------------------------------------
set.seed(12345)
models_videos <- list(
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans * (video_type_placebo + video_type_treatment),
    data = r2_choices_num,
    fixef = NULL,
    cluster = "group_id",
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_omit = "stratum_id|Intercept",
    control_group = list("video_type_control" = c("video_type_placebo", "video_type_treatment"))
  ),
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt * (video_type_placebo + video_type_treatment + public_non_observer + public_observer + discussion_pair_listener + discussion_pair_speaker + discussion_full + stratum_id + phase) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num,
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "pair_includes_trans_alt$|video|pair_includes_trans_alt\\:video",
    control_group = list("video_type_control" = c("video_type_placebo", "video_type_treatment"))
  ),
  "Chose trans in private\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + video_type_treatment + public_non_observer + public_observer + discussion_pair_listener + discussion_pair_speaker + discussion_full + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(pair_includes_trans == 1),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "video",
    control_group = list("video_type_control" = c("video_type_placebo", "video_type_treatment"))
  )
)

update_lasso_controls(models_videos, table_name = "Effect of rights videos\n(Table 2)",
                      remove = "Phase 2 effects\n(Table 2)")

p_vals_videos <- models_videos %>%
  map(get_comparison_p_vals,
      dummy_vars = c("video_type_placebo", "video_type_treatment", "video_type_control"),
      ri = ri,
      n_sims = ri_sims,
      stratum = "stratum_id",
      var_types = var_types_spec,
      coef_keep = "video_type",
      control_group = list("video_type_control" = c("video_type_placebo", "video_type_treatment"))

  )

p_vals_videos[[1]] <- p_vals_videos[[1]][3, ]
p_vals_videos[[2]] <- p_vals_videos[[2]][3, ]
p_vals_videos[[3]] <- p_vals_videos[[3]][1, ]


p_vals_videos <- p_vals_videos %>%
  map(~ .x %>%
    mutate(term = str_replace(term, "pair_includes_trans(_alt)?\\:", "")) %>%
    mutate(base = str_replace(base, "pair_includes_trans(_alt)?\\:", "")) %>%
    mutate(across(c(base, term), coef_label)))


# GET P-val comparing video vs discussion

# a. First model, add in discussion_full
model_videos_comp_1 <- feols_custom(
  r2_choose_comparator ~ pair_includes_trans * (video_type_placebo + video_type_treatment + public_non_observer + public_observer + discussion_pair_listener + discussion_pair_speaker + discussion_full),
  data = r2_choices_num,
  fixef = NULL,
  cluster = "group_id",
  ri = FALSE
)


p_vals_vid_discuss <- list(
  model_videos_comp_1 %>% get_diff_p_vals("pair_includes_trans:video_type_treatment", "pair_includes_trans:discussion_full"),
  models_videos[[2]] %>% get_diff_p_vals("pair_includes_trans_alt:video_type_treatment", "pair_includes_trans_alt:discussion_full"),
  models_videos[[3]] %>% get_diff_p_vals("video_type_treatment", "discussion_full")
) %>%
  map(
    ~ .x %>%
      mutate(term = str_replace(term, "pair_includes_trans(_alt)?\\:", "")) %>%
      mutate(base = str_replace(base, "pair_includes_trans(_alt)?\\:", "")) %>%
      mutate(across(c(base, term), coef_label))
  )

# Combine p-vals
p_vals_videos_comb <- map2(
  p_vals_videos,
  p_vals_vid_discuss,
  ~ bind_rows(.x, .y)
)

tex_export(
  models_videos,
  file = "outputs/tables/videos.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  dep_means = list(
    "Mean: Control video + worker is non-trans" = "video_type == 'control' & pair_includes_trans == FALSE",
    "Mean: Control video + worker is T" = "video_type == 'control' & pair_includes_trans == TRUE"
  ),
  coef_omit = vars_to_regex(c("stratum_id", "treat_type_r2", "public|discussion", "Intercept", "group_control", "pair_includes_trans_alt$", control_vars, "delivery", "phase")),
  add_rows = combine_add_rows(
    list_to_add_rows(control_rows),
    p_vals_to_add_rows(p_vals_videos_comb)
  )

)

# Effect of placebo
(models_videos[[1]]$coefficients["pair_includes_trans:video_type_placebo"] * 100) %>%
  write_stat("outputs/stats/effect_messaging_video.tex", digits = 1)

# effect of law
(models_videos[[1]]$coefficients["pair_includes_trans:video_type_treatment"] * 100) %>%
  write_stat("outputs/stats/effect_law_video.tex", digits = 1)

# p-value (minimum) of placebo vs law
p_vals_videos %>% bind_rows() %>%
  pull(p.value) %>%
  min() %>%
  write_stat("outputs/stats/p_val_min_video_messaging_vs_law.tex", digits = 2)

# p-value (maximum) of placebo vs law
p_vals_videos %>% bind_rows() %>%
  pull(p.value) %>%
  max() %>%
  write_stat("outputs/stats/p_val_max_video_messaging_vs_law.tex", digits = 2)


# effect of law vs discussion
(models_videos[[1]] %>% get_coeff("pair_includes_trans:video_type_treatment") / model_list[[1]] %>% get_coeff("pair_includes_trans:discussion_full")) %>%
  write_percentage("outputs/stats/effect_law_vs_discussion.tex", digits = 0)


(model_list[[1]] %>% get_coeff("pair_includes_trans:discussion_full") / models_videos[[1]] %>% get_coeff("pair_includes_trans:video_type_treatment")) %>%
  write_stat("outputs/stats/effect_discussion_vs_law.tex", digits = 1)


p_vals_vid_discuss %>% map("p.value") %>% unlist %>%
  write_range("outputs/stats/p_val_vid_vs_discussion.tex", digits = 1)

# Videos - by gender ---------------------------------------------------------------

r2_choices_num %>%

  group_by(discuss_type, pair_includes_female, pair_includes_trans) %>%
  mutate(
    pair_type = case_when(
      pair_includes_trans == 1 ~ "Trans",
      pair_includes_female == 1 ~ "Female",
      TRUE ~ "Male"
    )
  ) %>%

  bar_chart(x = video_type, y = r2_choose_comparator, fill = pair_type)

# Videos - figure ---------------------------------------------------------------


r2_n_video <- r2_choices %>%
  group_by(video_type, pair_includes_trans) %>%
  summarise(n_ind = n_distinct(ind_id),
            n_obs = n(),
            n_groups = n_distinct(group_id)) %>%
  mutate(video = str_replace(video_type, "treatment", "treat"))

r2_choices_num %>%
  mutate(
    control_non_trans = !pair_includes_trans & video_type_control == 1,
    placebo_non_trans = !pair_includes_trans & video_type_placebo == 1,
    treat_non_trans = !pair_includes_trans & video_type_treatment == 1,

    control_trans = pair_includes_trans & video_type_control == 1,
    placebo_trans = pair_includes_trans & video_type_placebo == 1,
    treat_trans = pair_includes_trans & video_type_treatment == 1,
  ) %>%
  mutate(across(c(control_non_trans, placebo_non_trans, treat_non_trans, control_trans, placebo_trans, treat_trans), as.numeric)) %>%

  feols_custom(
    r2_choose_comparator ~ (placebo_non_trans + treat_non_trans + control_trans + placebo_trans + treat_trans) + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown,
    data = .,
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "treat_type_r2_label", "phase"),
    cluster = "group_id",
  ) %>%
  tidy_90 %>%

  filter(str_detect(term, "(control|placebo|treat)_(non_)?trans$")) %>%
  add_row(term = "control_non_trans", estimate = 0, std.error = 0) %>%

  mutate(
    pair_includes_trans = str_detect(term, "control_trans|treat_trans|placebo_trans"),
    video = str_match(term, "(control|placebo|treat)_")[, 2],
  ) %>%
  left_join(r2_n_video, by = c("video", "pair_includes_trans")) %>%
  mutate(
    video_type_label = fct_recode(video, "Legal Rights" = "treat", "Rights Messaging" = "placebo", "Control" = "control") %>%
      fct_relevel("Control", "Rights Messaging") %>%
      fct_label_append(lab_append = paste0("\n(N=", n_ind, ")")),
    pair_includes_trans_label = ifelse(pair_includes_trans, "Worker\nis trans", "Worker\nis non-trans") %>% paste0(., "\n(Obs=", n_obs, ")")
  ) %>%
  filter(term != "placebo_non_trans", term != "treat_non_trans") %>%

  ggplot(aes(x = pair_includes_trans_label, colour = video)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "skyblue") +
  geom_errorbar(aes(ymin = conf.low_90, ymax = conf.high_90), position = position_dodge(0.8), show.legend = F, width = 0, size = 1.2) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), position = position_dodge(0.8), show.legend = F, width = 0, size = 0.7) +
  geom_point(aes(y = estimate), shape = 17, position = position_dodge(0.8), show.legend = F, size = 3) +
  scale_color_brewer(palette = "Set2") +
  theme_minimal() +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major = element_blank()) +
  labs(x = element_blank(), y = "Effect on probability of selecting worker\n in outcome round") +
  facet_wrap(~ video_type_label, scales = "free_x")


ggsave("outputs/figs/effect_videos_point.pdf", width = 5.5, height = 4)

# Restrict to control videos only ---------------------------------------------------------------

models_control_vid <- list(
  "Chose worker in\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans * (discussion_full),
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(video_type == "control"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Chose worker in\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt * (video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(video_type == "control"),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options =  list(
      potential_controls = control_vars,
      t = c("discussion_full"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Chose trans in\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)) %>% filter(video_type == "control"),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options =  list(
      potential_controls = control_vars,
      t = c("discussion_full"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    coef_omit = "stratum_id|Intercept|video"
  )
)

models_control_vid[[3]] %>% tidy %>% print_all

tex_export(
  models_control_vid,
  file = "outputs/tables/control_vid_only.tex",
  coef_rename = coef_label,
  coef_reorder = c("pair_includes_trans:discussion_full"),
  column_widths = list(4, "1.5in"),
  gof_map = fe_label_no_fe,
  coef_omit = vars_to_regex(c("group_control", "pair_includes_trans_alt$", "stratum_id", "Intercept", "video", "phase", control_vars, "delivery")),
  dep_means = list(
    "Mean: No discussion (private) + worker is non-trans" = "discuss_type == 'control' & pair_includes_trans == FALSE",
    "Mean: No discussion (private) + worker is T" = "discuss_type == 'control' & pair_includes_trans == TRUE"
  ),
  add_rows = list_to_add_rows(control_rows)
)

# Video effects on mechanisms ---------------------------------------------------------------

att_videos <- list(
  "# statements agreed with\n(list experiment)" = feols_custom(
    list_answer ~ trans_in_list_group + video_type_placebo + video_type_treatment + trans_in_list_group:(video_type_placebo + video_type_treatment) + factor(list_order_first) + list_b + trans_in_list_group,
    fixef = c("ind_id", "stratum_id", "treat_type_r2_label", "phase", "list_b"),
    cluster = ~ ind_id,
    data = list_exp,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "trans_in_list_group",
      group_control = FALSE
    ),
    warn = FALSE, notes = FALSE
  ),
  "Disapproves of\ndiscrimination (=1)" = feols_custom(
    attitude_val ~ video_type_placebo + video_type_treatment,
    fixef = c("stratum_id", "attitude_type", "treat_type_r2_label", "phase"),
    cluster = ~ ind_id,
    data = atts_long,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = FALSE
    )
  ),
  "Likely or very likely\nto complete delivery (=1)" = feols_custom(
    likely ~ trans_photo_yn * group_label  + factor(stratum_id) * trans_photo_yn + (video_type_placebo + video_type_treatment) * trans_photo_yn,
    fixef = c("ind_id", "stratum_id", "likelihood_order", "treat_type_r2_label", "phase"),
    cluster = ~ ind_id,
    data = likelihood,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "trans_photo_yn",
      group_control = FALSE
    )
  )
)

# likelihood$video_type_control

tex_export(
  att_videos,
  file = "outputs/tables/attitudes_videos.tex",
  coef_omit = vars_to_regex("stratum_id|Intercept|discuss|treat_type|phase", control_vars_except_items_reliability),
  coef_rename = coef_label,
  gof_map = fe_label %>% mutate(fmt = ifelse(raw == "Control mean", 2, fmt)) %>%
    filter(!str_detect(raw, "LASSO|stratum|video|phase")),
  controls_row = TRUE)

# p-val - effect of messaging on beliefs
att_videos[[3]] %>%
  get_p_val("trans_photo_yn:video_type_placebo") %>%
  write_stat("outputs/stats/p_effect_placebo_beliefs.tex")

# p-val - effect of law on beliefs
att_videos[[3]] %>%
  get_p_val("trans_photo_yn:video_type_treatment") %>%
  write_stat("outputs/stats/p_effect_law_beliefs.tex")


mean_belief_control_vid <- att_videos[[3]]$data %>%
  filter(trans_photo_yn == 1, video_type == "control") %>%
  pull(likely) %>%
  mean_na()

effect_belief_vid <- att_videos[[3]]$coefficients[c("trans_photo_yn:video_type_placebo", "trans_photo_yn:video_type_treatment")] %>%
  mean_na()

(effect_belief_vid *100)%>% write_stat("outputs/stats/effect_belief_vid.tex", digits = 0)

(effect_belief_vid / mean_belief_control_vid) %>% write_percentage("outputs/stats/effect_belief_vid_perc.tex")

# Video - norms ---------------------------------------------------------------

sn$public_non_observer

norms_videos <- list(
  "Predicted % who pick trans\n(community)" = feols_custom(
    n3 ~ video_type_placebo + video_type_treatment + discussion_full + discussion_pair_speaker + discussion_pair_listener + public_non_observer + public_observer,
    fixef = c("stratum_id", "phase", "norms_right"),
    cluster = ~ ind_id,
    data = sn,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = FALSE
    )
  ),
  "Predicted % who pick trans\n(within group-of-3)" = feols_custom(
    fml = group_predic_choose_trans ~ video_type_placebo + video_type_treatment + discussion_full + discussion_pair_speaker + discussion_pair_listener + public_non_observer + public_observer + item_diff + reliability_diff + reliability_shown,
    fixef = c("stratum_id", "phase"),
    cluster = "group_id",
    data = group_predic %>% filter(!is.na(group_predic_choose_trans)),
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = FALSE
    )
  )
)

p_vals_vids_norms <- norms_videos %>%
  map(~ get_comparison_p_vals(.x, dummy_vars = c("video_type_control", "video_type_placebo", "video_type_treatment"))) %>%
  map(filter, row_number() == 2) %>%
  map(mutate, across(c(base, term), coef_label))

p_vals_vids_norms_vs_discussion <- norms_videos %>%
  map(~get_diff_p_vals(.x, "video_type_treatment", "discussion_full")) %>%
  map(mutate, across(c(base, term), coef_label))

tex_export(
  norms_videos,
  file = "outputs/tables/norms_videos.tex",
  coef_omit = vars_to_regex("stratum_id|Intercept|discuss|item|reliability|public", control_vars_except_items_reliability),
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  add_rows = p_vals_to_add_rows(map2(p_vals_vids_norms, p_vals_vids_norms_vs_discussion, ~bind_rows(.x, .y))),
  controls_row = TRUE
)

# COMPARE effect of discusion vs effect of video

# Get p-value for difference between video and discussion
p_vals_vids_norms_vs_discussion[[1]]$p.value %>%
  # models_diff_video_norms[[1]] %>% get_p_val("video_type_treatment") %>%
  write_stat("outputs/stats/p_norm_community_diff_vid_discuss.tex", p_value = TRUE)

p_vals_vids_norms_vs_discussion[[2]]$p.value %>%
  # models_diff_video_norms[[2]] %>% get_p_val("video_type_treatment") %>%
  write_stat("outputs/stats/p_norm_group_diff_vid_discuss.tex", p_value = TRUE)

# Combine video mechanism results ----------------------------------------------------------------------------------------------

# Combine norm and attitude video results into single table
combined_video_results <- c(
  norms_videos,
  att_videos
) %>% 
set_names(
  c("Predicted\n% who pick\ntrans\n(community)",
    "Predicted\n% who pick\ntrans\n(within\ngroup-of-3)",
    "# statements\nagreed with\n(list experiment)",
    "Disapproves of\ndiscrimination\n(=1)",
    "Likely or very likely\nto complete\ndelivery\n(=1)"
  )
)

tex_export(
  combined_video_results,
  file = "outputs/tables/video_mechs_combined.tex",
  coef_omit = vars_to_regex("stratum_id|Intercept", control_vars, "reliability", "discussion", "public"),
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
)

# Beliefs about the law ---------------------------------------------------------------

law_check_models <- list(
  "Say trans have legal status\n(=1)" = feols_custom(
    k2 ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  ),

  "Say trans have legal status\n+ correctly name at least\none legal right (=1)" = feols_custom(
    k2_strict ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  ),
  "Number of legal rights\ncorrectly named" = feols_custom(
    k2.2_n_rights_do ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  ),

  "Not employing is illegal\n(=1)" = feols_custom(
    k4_illegal ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  ),

  "Avoiding on street is illegal\n(=1)" = feols_custom(
    k6.2_illegal ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  ),

  "Summary index (Z)" = feols_custom(
    law_index_z ~ video_type_placebo + video_type_treatment + factor(stratum_id),
    data = laws_atts,
    fixef = c("treat_type_r2_label", "phase", "stratum_id", "delivery_incentive_exp"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = TRUE
    )
  )
)

df %>%
  count_prop(phase, survey_date)

df %>%
  count_prop(phase, discuss_type)



tex_export(
  law_check_models,
  file = "outputs/tables/law_checks.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe %>% filter(raw != "nobs"),
  controls_row = TRUE,
  coef_omit = vars_to_regex("stratum_id|Intercept", control_vars),
  dep_means = list(
    "Mean: Control video" = "video_type == 'control'"
  )
)

fe_label %>% print_all

# Control awareness levels
laws_atts %>%
  filter(video_type == "control") %>%
  summarise(mean_na(k2_strict)) %>%
  .[[1]] %>%
{1 - .} %>%
  write_percentage("outputs/stats/control_awareness.tex")




# Interactions - table ---------------------------------------------------------------

video_discuss_interactions <- list(
  "3-person discussion\n+ No discussion (private)\n(Phases 1 + 2)" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * discussion_full + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(discuss_type %in% c("control", "discussion_full")),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  ),
  "All discussion arms\nexcept listeners\n(Phase 2 only)" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * (public + discussion_listener + discussion_speaker + discussion_full) + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(phase == "phase_2"),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  )
)

p_video_discuss_interaction_1 <- video_discuss_interactions[1] %>%
  map(get_comparison_p_vals, dummy_vars = c("discussion_full", "control"), keep_vars = "video_type") %>%
  map(~ filter(., str_detect(term, "video_type_(placebo|treatment)$")))

p_video_discuss_interaction_2 <- video_discuss_interactions[2] %>%
  map(get_comparison_p_vals, dummy_vars = c("control", "public", "discussion_listener", "discussion_speaker", "discussion_full"), keep_vars = "video_type") %>%
  map(~ filter(., str_detect(term, "video_type_(placebo|treatment)$")))

p_vals_video_discuss_interactions <- c(p_video_discuss_interaction_1, p_video_discuss_interaction_2) %>%
  map(~ mutate(., across(c(base, term), coef_label))) %>%
  map(~ mutate(., label = str_glue("$p$-val: ({term} $\\vert$ {base})"))) %>%
  map(select, label, p.value) %>%
  reduce(full_join, by = "label") %>%
  .[c(1, 3, 5, 7, 2, 4, 6, 8), ] %>%
  print

tex_export(
  video_discuss_interactions,
  file = "outputs/tables/video_interactions.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_omit = vars_to_regex("stratum_id|Intercept|item_diff|r2_reliability_diff|r2_reliability_shown", control_vars),
  add_rows = p_vals_video_discuss_interactions,
  additional_header = vec_to_custom_header(
    c(" ", rep("Chose trans in private outcome round (pairs with trans only) (=1)", 2))
  ),
  controls_row = TRUE
)


p_vals_video_discuss_interactions %>%
  filter(row_number() %in% c(1, 3)) %>%
  .[, 2:3] %>% unlist() %>%
  write_range("outputs/stats/p_vals_video_placebo_discussion.tex", digits = 2)


# P-values for law  / discussion interactions
c(
  video_discuss_interactions[[1]] %>% get_p_val("video_type_treatment:discussion_full"),
  video_discuss_interactions[[2]] %>% get_p_val("video_type_treatment:discussion_full")
) %>%
  write_range("outputs/stats/p_vals_vid_discuss_interact.tex", digits = 2)

# MULTIPLE HYPOTHESIS TESTING
video_discuss_interactions[[1]] %>% tidy_90 %>% filter(str_detect(term, "video_type|discussion_full")) %>%
  mutate(q_vals = q_val(p_vals = p.value, q_res = 0.0001))

# Diff between discussion and vids ---------------------------------------------------------------

video_discuss_diffs <- list(
  "3-person discussion\n+ No discussion (private)\n(Phases 1 + 2)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + I(video_type_treatment + discussion_full) + discussion_full + factor(stratum_id),
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(discuss_type %in% c("control", "discussion_full")),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_omit = "stratum_id|Intercept",
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  ),

  "3-person discussion\n+ No discussion (private)\n(Phases 1 + 2)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + I(video_type_treatment + discussion_full) + discussion_full + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(discuss_type %in% c("control", "discussion_full")),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  ),
  "All discussion arms\nexcept listeners\n(Phase 2 only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + I(video_type_treatment + discussion_pooled) + discussion_pooled + public + factor(stratum_id),
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(phase == "phase_2", !(is_listener %in% 1)),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_omit = "stratum_id|Intercept",
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  ),
  "All discussion arms\nexcept listeners\n(Phase 2 only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + I(video_type_treatment + discussion_pooled) + discussion_pooled + public + factor(stratum_id) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(pair_includes_trans == 1) %>% filter(phase == "phase_2", !(is_listener %in% 1)),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE, lasso_options = lasso_options_vids_discuss
  )
)

video_discuss_diffs[1:2] %>%
  map(~ get_p_val(., "discussion_full"))

# Interactions - norms ---------------------------------------------------------------

norms_videos_interactions <- list(
  "Predicted % who pick trans\n(community)" = feols_custom(
    n3 ~ (video_type_placebo + video_type_treatment) * (discussion_full),
    fixef = c("stratum_id", "phase", "delivery_incentive_exp", "norms_right"),
    cluster = ~ ind_id,
    data = sn %>% filter(discuss_type %in% c("control", "discussion_full")),
    lasso = TRUE,
    lasso_options = lasso_options_vids_discuss
  ),
  "Predicted % who pick trans\n(community)" = feols_custom(
    n3 ~ (video_type_placebo + video_type_treatment) * (discussion_pooled),
    fixef = c("stratum_id", "phase", "delivery_incentive_exp", "norms_right"),
    cluster = ~ ind_id,
    data = sn %>% filter(phase == "phase_2", !(is_listener %in% 1)),
  ),
  "Predicted % who pick trans\n(within group-of-3)" = feols_custom(
    fml = group_predic_choose_trans ~ (video_type_placebo + video_type_treatment) * discussion_full,
    fixef = c("stratum_id", "phase", "delivery_incentive_exp"),
    cluster = ~ group_id,
    data = group_predic %>% filter(discuss_type %in% c("control", "discussion_full"))
  ),
  "Predicted % who pick trans\n(within group-of-3)" = feols_custom(
    fml = group_predic_choose_trans ~ (video_type_placebo + video_type_treatment) * (discussion_pooled + public),
    fixef = c("stratum_id", "phase", "delivery_incentive_exp"),
    cluster = ~ group_id,
    data = group_predic %>% filter(phase == "phase_2", !(is_listener %in% 1))
  )
)


p_vals_video_discuss_interactions_norms <- c(
  norms_videos_interactions[c(1, 3)] %>%
    map(get_comparison_p_vals, dummy_vars = c("discussion_full", "control"), keep_vars = "video_type") %>%
    map(~ filter(., str_detect(term, "video_type_(placebo|treatment)$"))),
  norms_videos_interactions[c(2, 4)] %>%
    map(get_comparison_p_vals, dummy_vars = c("control", "discussion_pooled"), keep_vars = "video_type") %>%
    map(~ filter(., str_detect(term, "video_type_(placebo|treatment)$")))
) %>%
  map(~ mutate(., across(c(base, term), coef_label))) %>%
  map(~ mutate(., label = str_glue("p-val: {term} $\\vert$ {base}"))) %>%
  map(select, label, p.value) %>%
  reduce(full_join, by = "label") %>%
  # .[c(1, 3, 5, 2, 4, 6), ] %>%
  print


tex_export(
  norms_videos_interactions,
  file = "outputs/tables/video_interactions_norms.tex",
  coef_rename = coef_label,
  gof_map = fe_label,
  coef_omit = "stratum_id|Intercept|item_diff|r2_reliability_diff|r2_reliability_shown",
  add_rows = p_vals_video_discuss_interactions_norms
)

group_predic %>%
  filter(phase == "phase_2") %>%
  bar_chart(x = discussion_pooled, fill = video_type, y = group_predic_choose_trans)

group_predic %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  bar_chart(x = discussion_pooled, fill = video_type, y = group_predic_choose_trans)

# Interactions - figure ---------------------------------------------------------------

all_6_groups_1 <- r2_choices_num %>%
  filter(phase == "phase_2") %>%
  bar_chart(
    x = discuss_type_label,
    fill = video_type_label,
    y = r2_choose_trans,
    perc = TRUE
  ) +
  labs(x = element_blank(),
       y = "Probability of choosing trans in outcome round",
       fill = "Rights video:") +
  theme_bw()

all_6_groups_2 <- r2_choices_num %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  bar_chart(
    x = discuss_type_label,
    fill = video_type_label,
    y = r2_choose_trans,
    perc = TRUE
  ) +
  labs(x = element_blank(),
       y = "Probability of choosing trans in outcome round",
       fill = "Rights video:") +
  theme_bw()

ggarrange(
  all_6_groups_2,
  all_6_groups_1,
  labels = c("Phases 1+2", "Phase 2 only"),
  common.legend = TRUE,
  widths = c(2, 4),
  vjust = 2.5,
  label.x = 0.05
)

ggsave("outputs/figs/all_6_groups.pdf", width = 10 , height = 5)

messaging_vs_law_interact <- list(
  "Chose trans in\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_treatment * discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>%
      filter(discuss_type %in% c("control", "discussion_full")) %>%
      filter(video_type != "control") %>%
      filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  ),
  "Chose trans in\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_treatment * discussant + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>%
      filter(phase == "phase_2") %>%
      filter(video_type != "control") %>%
      filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id"
  )
)

tex_export(
  messaging_vs_law_interact,
  file = "outputs/tables/messaging_vs_law_interact.tex",
  coef_omit = "stratum_id|Intercept|item|reliability",
  coef_rename = coef_label,
  gof_map = fe_label %>% mutate(fmt = ifelse(raw == "Control mean", 2, fmt))
)


# Videos - confounder checks ---------------------------------------------------------------

confounder_models_video <- list(
  "Remembered word\n'transgender' (=1)\n(Phase 1 only)" = feols_custom(
    trans_remembered ~ video_type_placebo + video_type_treatment + prop_salience_minus_trans,
    fixef = c("stratum_id", "discuss_type", "phase", "delivery_incentive_exp"),
    cluster = ~ group_id,
    data = salience,
    lasso = TRUE,
    lasso_options = lasso_options_no_interact
  ),
  "Correctly guess purpose (=1)\n(after main outcome)" = feols_custom(
    purpose_0_trans ~ video_type_placebo + video_type_treatment,
    fixef = c("stratum_id", "discuss_type", "phase", "delivery_incentive_exp"),
    cluster = ~ group_id,
    data = purpose,
    lasso = TRUE,
    lasso_options = lasso_options_no_interact
  ),
  "Correctly guess purpose (=1)\n(end of experiment)" = feols_custom(
    purpose_2_trans ~ video_type_placebo + video_type_treatment,
    fixef = c("stratum_id", "discuss_type", "phase", "delivery_incentive_exp"),
    cluster = ~ group_id,
    data = purpose,
    lasso = TRUE,
    lasso_options = lasso_options_no_interact
  )
)

tex_export(
  confounder_models_video,
  file = "outputs/tables/confounders_videos.tex",
  coef_omit = vars_to_regex("stratum_id|Intercept", control_vars),
  coef_rename = coef_label,
  gof_map = fe_label_no_fe %>% filter(raw != "nobs"),
  dep_means = list(
    "Mean: Control video" = "video_type == 'control'"
  ),
  controls_row = TRUE
)

confounder_interactions_vid <- list(
  "Phases 1 + 2" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * purpose_0_trans + (video_type_placebo + video_type_treatment)  + factor(stratum_id)+ item_diff + r2_reliability_shown * r2_reliability_diff,
    data = r2_with_purpose %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "treat_type_r2_label", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "purpose_0_trans",
      group_control = FALSE
    )
  ),
  "Phases 1 + 2" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * purpose_2_trans + video_type_placebo + video_type_treatment  + factor(stratum_id) + item_diff + r2_reliability_shown * r2_reliability_diff,
    data = r2_with_purpose %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "treat_type_r2_label", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "purpose_0_trans",
      group_control = FALSE
    )
  ),
  "Phase 1 only" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * high_sdb + video_type_placebo + video_type_treatment  + factor(stratum_id)+ item_diff + r2_reliability_shown * r2_reliability_diff,
    data = r2_with_sdb %>% filter(phase == "phase_1", !is.na(r2_choose_trans)),
    fixef = c("stratum_id", "treat_type_r2_label", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "high_sdb",
      group_control = FALSE
    )
  ),
  "Phase 1 only" = feols_custom(
    r2_choose_trans ~ (video_type_placebo + video_type_treatment) * (trans_remembered) + above_median_prop_salience + video_type_placebo + video_type_treatment  + factor(stratum_id)+ item_diff + r2_reliability_shown * r2_reliability_diff,
    data = r2_with_salience %>% filter(!is.na(r2_choose_trans), phase == "phase_1"),
    fixef = c("stratum_id", "treat_type_r2_label", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "trans_remembered",
      group_control = FALSE
    )
  )
)


tex_export(
  confounder_interactions_vid[1:2],
  file = "outputs/tables/confounder_interactions_video.tex",
  coef_omit = vars_to_regex("stratum_id|Intercept", control_vars),
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  controls_row = TRUE,
  additional_header = vec_to_custom_header(
    c(" ", rep("Chose trans in private outcome round (=1)", 2))
  )
)


# Videos >> anonymous choices ---------------------------------------------------------------

anon_videos <- list(
  "Chose worker in\nprivate pick-up round (=1)" = feols_custom(
    anon_choose_comparator ~ pair_includes_trans * (video_type_placebo + video_type_treatment),
    data = anon_choices, 
    fixef = NULL,
    cluster = "group_id",
    stratum = "stratum_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Chose worker in\nprivate pick-up round (=1)" = feols_custom(
    anon_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt * (video_type_placebo + video_type_treatment + stratum_id + discuss_type_label + delivery_incentive_exp),
    data = anon_choices,
    fixef = c("stratum_id", "delivery_incentive_exp", "treat_type_r2_label", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = "pair_includes_trans",
      group_control = FALSE
    ),
    stratum = "stratum_id",
    coef_omit = "stratum_id|Intercept|video"
  ),
  "Chose trans in\nprivate pick-up round (=1)" = feols_custom(
    anon_choose_comparator ~ stratum_id + video_type_placebo + video_type_treatment + delivery_incentive_exp,
    data = anon_choices %>% filter(pair_includes_trans==1),
    fixef = c("stratum_id", "delivery_incentive_exp", "treat_type_r2_label", "phase"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("video_type_placebo", "video_type_treatment"),
      interact = NULL,
      group_control = FALSE
    ),
    stratum = "stratum_id",
    coef_omit = "stratum_id|Intercept|video"
  )
)


p_vals_videos_anonymous <- anon_videos %>%
  map(
    ~ get_comparison_p_vals(.x,
                            dummy_vars = c("video_type_control", "video_type_placebo", "video_type_treatment"),
                            ri = FALSE,
    )
  ) %>%
  purrr::map(filter_interact_terms,
             vars = c("video_type_control", "video_type_placebo", "video_type_treatment"),
             interact = "pair_includes_trans(_alt)?") %>%
  map(filter, paste0(base, "_", term) %in% c(
    "video_type_placebo_video_type_treatment"
  )) %>%
  purrr::map(~ mutate(.x, across(c(base, term), ~ .x %>% coef_label)))

tex_export(
  anon_videos,
  file = "outputs/tables/anon_choices_videos.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_omit = vars_to_regex("stratum_id|Intercept|discuss|delivery|b11|bachelors|pair_includes_trans_alt$|dq12",
                            control_vars_except_items_reliability),
  add_rows = combine_add_rows(
    list_to_add_rows(control_rows),
    p_vals_to_add_rows(p_vals_videos_anonymous)
  )
)

anon_videos